package com.jysz.certificate.persistence.model.cm;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.jysz.certificate.persistence.enums.ReleaseState;
import com.jysz.certificate.persistence.model.mf.BillOption;
import com.jysz.certificate.persistence.model.system.Brand;
import com.jysz.certificate.persistence.model.system.Financial;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.webcore.ssh.entity.AbstractBaseEntity;

/**
 * 释放单
 * @author xuxiaolong
 */
@Entity
@Table(name="cm_release_")
@org.hibernate.annotations.Entity
public class Release extends AbstractBaseEntity<Long> {
	
	private static final long serialVersionUID = -2168956211139583647L;

	/** 金融机构*/
	private Financial financial;
	
	/** 店铺*/
	private Shop shop;
	
	/** 主机厂*/
	private Brand brand;
	
	/** 申请单号( 店号+日期+当天发起的第几次释放单 _拆分次数)*/
	private String applyNum;
	
	/** 申请人*/
	private User applyUser;
	
	/** 释放单号*/
	private String releaseNum;
	
	/** 申请时间*/
	private Date applyDate;
	
	/** 释放时间*/          
	private Date releaseDate;

	/** 释放单状态*/
	private ReleaseState state;
	
	/** 父级释放单*/
	private Release parent;
	
	/** 拆分释放单*/
	private Release split;
	
	/** 根（店号+日期+当天发起的第几次释放单）*/
	private String root;
	
	/** 深度*/
	private Integer depth;
	
	/** 释放单包含的合格证*/
	private Set<Certification> certifications;

	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)  
	@JoinColumn(name = "_financial_id",insertable=true, updatable=true,columnDefinition="bigint(20)")
	public Financial getFinancial() {
		return financial;
	}

	public void setFinancial(Financial financial) {
		this.financial = financial;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)  
	@JoinColumn(name = "_shop_id",insertable=true, updatable=true,columnDefinition="bigint(20)")
	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)  
	@JoinColumn(name = "_brand_id",insertable=true, updatable=true,columnDefinition="bigint(20)")
	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	@Column(name= "_apply_num",nullable=false,columnDefinition="varchar(255)")
	public String getApplyNum() {
		return applyNum;
	}

	public void setApplyNum(String applyNum) {
		this.applyNum = applyNum;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)  
	@JoinColumn(name = "_apply_user_id",insertable=true, updatable=true,columnDefinition="bigint(20)")
	public User getApplyUser() {
		return applyUser;
	}

	public void setApplyUser(User applyUser) {
		this.applyUser = applyUser;
	}

	@Column(name= "_release_num",nullable=true,columnDefinition="varchar(255)")
	public String getReleaseNum() {
		return releaseNum;
	}

	public void setReleaseNum(String releaseNum) {
		this.releaseNum = releaseNum;
	}

	@Column(name= "_apply_date",nullable=false)
	public Date getApplyDate() {
		return applyDate;
	}

	public void setApplyDate(Date applyDate) {
		this.applyDate = applyDate;
	}

	@Column(name= "_release_date",nullable=true)
	public Date getReleaseDate() {
		return releaseDate;
	}

	public void setReleaseDate(Date releaseDate) {
		this.releaseDate = releaseDate;
	}

	@Column(name = "_state", nullable = false,columnDefinition="int(11)")
	@Enumerated(EnumType.ORDINAL)
	public ReleaseState getState() {
		return state;
	}

	public void setState(ReleaseState state) {
		this.state = state;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)  
	@JoinColumn(name = "_parent",columnDefinition="bigint(20)")
	public Release getParent() {
		return parent;
	}

	public void setParent(Release parent) {
		this.parent = parent;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.MERGE)  
	@JoinColumn(name = "_split",columnDefinition="bigint(20)")
	public Release getSplit() {
		return split;
	}

	public void setSplit(Release split) {
		this.split = split;
	}

	@Column(name= "_root",nullable=false,columnDefinition="varchar(255)")
	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	@Column(name= "_depth",nullable=false,columnDefinition="int(11)")
	public Integer getDepth() {
		return depth;
	}

	public void setDepth(Integer depth) {
		this.depth = depth;
	}

	@OneToMany(targetEntity = Certification.class,cascade = {CascadeType.PERSIST })
	@JoinColumn(name="_release_id")
	public Set<Certification> getCertifications() {
		return certifications;
	}

	public void setCertifications(Set<Certification> certifications) {
		this.certifications = certifications;
	}
	
	/**begin properties column constan**/
	public static final String APPLYDATE = "applyDate";
	public static final String RELEASEDATE = "releaseDate";
	public static final String STATE = "state";
	/**end properties column constan**/
	
}
